var  {Global variables}
  RoiLeft,RoiTop,RoiWidth,RoiHeight:integer;
  GelPid,MarkedPid,nLanes,count,ppv:integer;
  SaveRoiLeft,SaveRoiTop,hOffset,vOffset:integer;
  MinY,MaxY,xscale,yscale:real;
  VerticalLanes,Uncalibrated:boolean;

procedure MarkLane;
var
  left,top,width,height:integer;
  GelNotCalibrated:boolean;
  min,max:real;
begin
  GetRoi(left,top,width,height);
  if width=0 then begin
    PutMessage('Please outline the next lane.');
    exit;
  end;
  if (VerticalLanes and (left=RoiLeft))
      or  (not VerticalLanes and (top=RoiTop)) then begin
     PutMessage('This lane has already been marked.');
     exit;
  end;
  nLanes:=nLanes+1;
  ChoosePic(GelPid);
  if VerticalLanes then begin
     MakeRoi(left+hOffset,RoiTop+vOffset,RoiWidth,RoiHeight);
     RoiLeft:=left;
     rUser1[nLanes]:=left;
  end else begin
     MakeRoi(RoiLeft+hOffset,top+vOffset,RoiWidth,RoiHeight);
     RoiTop:=top;
     rUser1[nLanes]:=top;
  end;
  GetPlotData(count,ppv,min,max);
  if min<MinY then MinY:=min;
  if max>MaxY then MaxY:=max;
  SelectPic(MarkedPid);
  if VerticalLanes
     then MakeRoi(left,RoiTop,RoiWidth,RoiHeight)
     else MakeRoi(RoiLeft,top,RoiWidth,RoiHeight);
  DrawBoundary;
  SetFontSize(9);
  if VerticalLanes then begin
    SetText('Centered, No Background');
    MoveTo(RoiLeft+RoiWidth/2,RoiTop-6);
  end else begin
    SetText('Right Justified, No Background');
    MoveTo(RoiLeft-2,RoiTop+RoiHeight/2);
  end;
  Write(nLanes:1);
  RestoreRoi;
  SelectPic(MarkedPid);
end;


macro 'Mark First Lane [1]';
var
  GelWidth,GelHeight,left,width:integer;
  margin,leftm,rightm,topm,bottomm:integer;
begin
  RequiresVersion(1.54);
  if WindowTitle='Temp' then begin
    PutMessage('To restart, select the first lane in the gel.');
    exit;
  end;
  GetRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  if RoiWidth=0 then begin
    PutMessage('The Setup macro expects the Gel to be displayed and the first lane outlined.');
    exit;
  end;
  VerticalLanes:=RoiHeight>RoiWidth;
  Uncalibrated:=not Calibrated;
  GelPid:=PidNumber;
  SetForegroundColor(255);
  SetBackgroundColor(0);
  MinY:=999999;
  MaxY:=-999999;
  nLanes:=0;
  margin:=20;
  GetPicSize(GelWidth,GelHeight);
  leftm:=RoiLeft;
  if leftm>margin then leftm:=margin;
  rightm:=GelWidth-(RoiWidth+RoiLeft);
  if rightm>margin then rightm:=margin;
  bottomm:=GelHeight-(RoiHeight+RoiTop);
  if bottomm>margin then bottomm:=margin;
  topm:=RoiTop;
  if topm>margin then topm:=margin;
  if VerticalLanes
    then MakeRoi(RoiLeft-leftm,RoiTop-topm,
           GelWidth-RoiLeft+leftm,RoiHeight+topm+bottomm)
    else MakeRoi(RoiLeft-leftm,RoiTop-topm,
           RoiWidth+leftm+rightm,GelHeight-RoiTop+topm);
  Duplicate('Temp');
  MarkedPid:=PidNumber;
  SaveRoiLeft:=RoiLeft;
  SaveRoiTop:=RoiTop;
  hOffset:=RoiLeft-leftm;
  vOffset:=RoiTop-topm;
  RoiLeft:=leftm;
  RoiTop:=topm;
  SelectPic(GelPid);
  KillRoi;
  SelectPic(MarkedPid);
  MakeRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  if VerticalLanes
    then RoiLeft:=-1
    else RoiTop:=-1;
  SetPlotScale(0,0);
  MarkLane;
end;

macro 'Mark Next Lane [2]';
var
  left,top,width,height:integer;
  GelNotCalibrated:boolean;
  min,max:real;
begin
  if nLanes=0 then begin
    PutMessage('Please use the "Mark First Lane" macro first.');
    Exit;
  end;
  MarkLane;
end;

macro 'Plot Lanes [3]';
var
  left,top,width,height:integer;
  GelNotCalibrated:boolean;
  i,LaneLength:integer;
  xmin,xmax,ymin,ymax,min,max:real;
  MaxPlotWindowHeight,NeededSize:integer;
  PlotTop,PlotBottom,PlotWidth,PlotHeight:integer;
  hMargin,vMargin,SavePlotTop,PlotWindowSize:integer;
  PlotWinHeight,PlotWinWidth,PlotsPid:integer;
begin
  if nLanes=0 then begin
    PutMessage('Please use the Setup and Mark macros first.');
    Exit;
  end;
  if VerticalLanes
    then LaneLength:=RoiHeight
    else LaneLength:=RoiWidth;
  PlotWidth:=LaneLength;
  if PlotWidth<500 then PlotWidth:=500;
  if PlotWidth>(2*LaneLength) then PlotWidth:=2*LaneLength;
  PlotHeight:=PlotWidth/2;
  if PlotHeight<200 then PlotHeight:=200;
  if PlotHeight>400 then PlotHeight:=400;
  MaxPlotWindowHeight:=800;
  hMargin:=5;
  vMargin:=5;
  PlotWinHeight:=nLanes*PlotHeight+2*vMargin;
  if PlotWinHeight>MaxPlotWindowHeight then begin
     PlotWinHeight:=MaxPlotWindowHeight;
     PlotHeight:=(PlotWinHeight-2*vMargin)/nLanes;
  end;
  PlotWinWidth:=PlotWidth+2*hMargin;
  PlotWindowSize:=PlotWinWidth*PlotWinHeight;
  if PlotWindowSize>UndoBufferSize then begin
    NeededSize:=(PlotWindowSize+0.05*PlotWindowSize) div 1024;
    PutMessage('Use Preferences(Options Menu) to increase the Undo buffer size to at least ',NeededSize:1,'K.');
    KillRoi;
    exit;
  end;
  SetForegroundColor(255);
  SetBackgroundColor(0);
  SetNewSize(PlotWinWidth,PlotWinHeight);
  MakeNewWindow('Plots');
  PlotsPid:=PidNumber;
  if UnCalibrated then begin
    SetText('Left Justified');
    SetFontSize(12);
    MoveTo(6,vmargin+1);
    Write('uncalibrated');
  end;
  xmin:=0;
  xmax:=count-1;
  ymin:=MinY;
  ymax:=MaxY;
  xscale:=PlotWidth/(xmax-xmin);
  yscale:=PlotHeight/(ymax-ymin);
  PlotTop:=vMargin;
  SavePlotTop:=PlotTop;
  RoiLeft:=SaveRoiLeft;
  RoiTop:=SaveRoiTop;
  SetLineWidth(1);
  for i:=1 to nLanes do begin
     PlotBottom:=PlotTop+(ymax-ymin)*yscale;
     ChoosePic(GelPid);
     if VerticalLanes
        then MakeRoi(rUser1[i]+hOffset,RoiTop,RoiWidth,RoiHeight)
        else MakeRoi(RoiLeft,rUser1[i]+vOffset,RoiWidth,RoiHeight);
     GetPlotData(count,ppv,min,max);
     SelectPic(PlotsPid);
     MoveTo(hMargin,PlotBottom);
     LineTo(PlotWinWidth-hMargin,PlotBottom);
     MoveTo(hMargin,PlotBottom-(PlotData[0]-ymin)*yscale);
     for i:=1 to count-1 do
        LineTo(hMargin+i*xscale,PlotBottom-(PlotData[i]-ymin)*yscale);
     PlotTop:=PlotTop+PlotHeight;
  end;
  nLanes:=0;
  ResetCounter;
  SetOptions('Area');
  WandAutoMeasure(true);
  AdjustAreas(true);
  LabelParticles(false);
  IncludeInteriorHoles(true);
  SetFontSize(9);
  SetText('Centered');
  if Uncalibrated
    then SetScale(0,'pixels')
    else SetScale(sqrt(xscale*yscale/ppv),'unit');
end;
